//
//  DetailsHesder.m
//  GiftNote
//
//  Created by lanou3g on 15/8/12.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "DetailsHesder.h"
#import "Screen.h"
#import "UIImageView+WebCache.h"
@interface DetailsHesder ()<UIScrollViewDelegate>
/**
 *  保存可见的视图
 */
@property (nonatomic, strong) NSMutableSet *visibleImageViews;

/**
 *  保存可重用的
 */
@property (nonatomic, strong) NSMutableSet *reusedImageViews;

/**
 *  滚动视图
 */
@property (nonatomic, weak) UIScrollView *scrollView;

/**
 *  所有的图片名
 */
@property (nonatomic, strong) NSArray *imageNames;



@property (nonatomic,retain) UILabel *nameLabel;
@property (nonatomic,retain) UILabel *moneyLabel;
@property (nonatomic,retain) UILabel *instructionsLabel;


@end
@implementation DetailsHesder

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 210 +100, 300, 30)];
        _nameLabel.font = [UIFont systemFontOfSize:25];
        _moneyLabel.textAlignment = 1;
       
        self.moneyLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 210 + 30 + 10 + 100, 100, 30)];
        _moneyLabel.textAlignment = 1;
        _moneyLabel.font = [UIFont systemFontOfSize:20];
       
        self.instructionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 210 + 30 + 10 + 30 + 10 + 100, 375 - 10 - 10, 70)];
        _instructionsLabel.textAlignment = 1;
        _instructionsLabel.numberOfLines = 10;
     
        [self addSubview:_nameLabel];
        [self addSubview:_moneyLabel];
        [self addSubview:_instructionsLabel];
   
        [self setupScrollView];

    }
    return self;
}


// 添加UIScrollView
- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor grayColor];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:scrollView];
    _scrollView = scrollView;
    
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self showImages];
}

#pragma mark - Private Method

- (void)showImages {
    
    // 获取当前处于显示范围内的图片的索引
    CGRect visibleBounds = self.scrollView.bounds;
    CGFloat minX = CGRectGetMinX(visibleBounds);
    CGFloat maxX = CGRectGetMaxX(visibleBounds);
    CGFloat width = CGRectGetWidth(visibleBounds);
    
    NSInteger firstIndex = (NSInteger)floorf(minX / width);
    NSInteger lastIndex  = (NSInteger)floorf(maxX / width);
    
    // 处理越界的情况
    if (firstIndex < 0) {
        firstIndex = 0;
       
    }
    
    if (lastIndex >= [self.imageNames count]) {
        
        
        lastIndex = [self.imageNames count] - 1;
       
    }
    
    // 回收不再显示的ImageView
    NSInteger imageViewIndex = 0;
    for (UIImageView *imageView in self.visibleImageViews) {
        imageViewIndex = imageView.tag;
        // 不在显示范围内
        if (imageViewIndex < firstIndex || imageViewIndex > lastIndex) {
            [self.reusedImageViews addObject:imageView];
            [imageView removeFromSuperview];
        }
    }
    
    [self.visibleImageViews minusSet:self.reusedImageViews];
    
    // 是否需要显示新的视图
    for (NSInteger index = firstIndex; index <= lastIndex; index++) {
        BOOL isShow = NO;
        
        for (UIImageView *imageView in self.visibleImageViews) {
            if (imageView.tag == index) {
                isShow = YES;
            }
        }
        if (!isShow) {
          [self showImageViewAtIndex:index];
        }
        
    }
}

// 显示一个图片view
- (void)showImageViewAtIndex:(NSInteger)index {
    
    UIImageView *imageView = [self.reusedImageViews anyObject];
    
    if (imageView) {
        [self.reusedImageViews removeObject:imageView];
    } else {
        imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    
    CGRect bounds = self.scrollView.bounds;
    CGRect imageViewFrame = bounds;
    imageViewFrame.origin.x = CGRectGetWidth(bounds) * index;
    imageView.tag = index;
    imageView.frame = imageViewFrame;
    [imageView sd_setImageWithURL:[NSURL URLWithString: self.imageNames[index]]];
    
    [self.visibleImageViews addObject:imageView];
    [self.scrollView addSubview:imageView];
}

#pragma mark - Getters and Setters

- (void)setDetailsHeaderModel:(ArtiollerModel *)detailsHeaderModel{
    _detailsHeaderModel = detailsHeaderModel;
    _nameLabel.text = detailsHeaderModel.name;
    _moneyLabel.text = [NSString stringWithFormat:@"￥:%@",_detailsHeaderModel.price];
    _instructionsLabel.text = [NSString stringWithFormat:@"%@",_detailsHeaderModel.description1];
    
    
    _imageNames = detailsHeaderModel.image_urls;
    _scrollView.contentSize = CGSizeMake(self.imageNames.count *CGRectGetWidth(_scrollView.frame), 0);
    [self showImageViewAtIndex:0];
}

- (NSMutableSet *)visibleImageViews {
    if (_visibleImageViews == nil) {
        _visibleImageViews = [[NSMutableSet alloc] init];
    }
    return _visibleImageViews;
}

- (NSMutableSet *)reusedImageViews {
    if (_reusedImageViews == nil) {
        _reusedImageViews = [[NSMutableSet alloc] init];
    }
    return _reusedImageViews;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
